Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./year2.RDS")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 428)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 11.41151 11.46353 11.51463 11.56485 11.61422 11.66275 11.71048 11.75743
## [9] 11.80363 11.84911 11.89388 11.93798 11.98144 12.02427 12.06651 12.10811
## [17] 12.14901 12.18919 12.22862 12.26728 12.30514 12.34218 12.37838 12.41372
## [25] 12.44816 12.48169 12.51429 12.54592 12.57657 12.60659 12.63627 12.66552
## [33] 12.69424 12.72234 12.74972 12.77628 12.80193 12.82658 12.85012 12.87245
## [41] 12.89350 12.91315 12.93131 12.94912 12.96756 12.98628 13.00495 13.02323
## [49] 13.04078 13.05726 13.07232 13.08564 13.09687 13.10568 13.11171 13.11506
## [57] 13.11615 13.11513 13.11219 13.10748 13.10118 13.09346 13.08447 13.07440
## [65] 13.06340 13.05164 13.03930 13.02654 13.01353 13.00043 12.98447 12.96329
## [73] 12.93776 12.90873 12.87703 12.84353 12.80906 12.77448 12.74064 12.70838
## [81] 12.67856 12.65202 12.62961 12.60821 12.58443 12.55873 12.53155 12.50335
## [89] 12.47457 12.44566 12.41708 12.38928 12.36270 12.33779 12.31501 12.29481
## [97] 12.27764 12.26218 12.24693 12.23206 12.21771 12.20403 12.19120 12.17935
## [105] 12.16866 12.15926 12.15175 12.14641 12.14301 12.14128 12.14097 12.14184
## [113] 12.14363 12.14610 12.14898 12.15203 12.15500 12.15763 12.15967 12.16088
## [121] 12.16238 12.16533 12.16950 12.17468 12.18065 12.18717 12.19404 12.20103
## [129] 12.20792 12.21449 12.22051 12.22577 12.23004 12.23310 12.23492 12.23573
## [137] 12.23571 12.23502 12.23384 12.23234 12.23070 12.22910 12.22770 12.22669
## [145] 12.22622 12.22649 12.22766 12.22991 12.23341 12.23834 12.24486 12.25316
## [153] 12.26340 12.27600 12.29086 12.30746 12.32523 12.34365 12.36217 12.38024
## [161] 12.39733 12.41289 12.42951 12.44983 12.47338 12.49968 12.52828 12.55868
## [169] 12.59043 12.62305 12.65607 12.68901 12.72141 12.75280 12.78270 12.81064
## [177] 12.83615 12.85876 12.87800 12.89339 12.90447 12.91473 12.92758 12.94240
## [185] 12.95857 12.97548 12.99250 13.00902 13.02442 13.03808 13.04938 13.05771
## [193] 13.06245 13.06298 13.05868 13.04961 13.03654 13.01989 13.00010 12.97760
## [201] 12.95281 12.92617 12.89811 12.86905 12.83944 12.80970 12.78026 12.75155
## [209] 12.72400 12.69102 12.64757 12.59641 12.54031 12.48202 12.42430 12.36992
## [217] 12.32164 12.28221 12.24616 12.20660 12.16420 12.11962 12.07353 12.02659
## [225] 11.97947 11.93283 11.88735 11.84368 11.80249 11.76445 11.73023 11.70048
## [233] 11.67260 11.64382 11.61453 11.58512 11.55598 11.52752 11.50013 11.47421
## [241] 11.45014 11.42833 11.40917 11.39306 11.38039 11.37106 11.36453 11.36059
## [249] 11.35898 11.35949 11.36188 11.36592 11.37137 11.37800 11.38558 11.39388
## [257] 11.40266 11.41170 11.42076 11.42960 11.44137 11.45868 11.48053 11.50592
## [265] 11.53385 11.56331 11.59331 11.62285 11.65092 11.67653 11.69866 11.71633
## [273] 11.73233 11.74998 11.76900 11.78914 11.81014 11.83172 11.85363 11.87561
## [281] 11.89739 11.91872 11.93932 11.95894 11.97732 11.99419 12.00972 12.02435
## [289] 12.03821 12.05141 12.06410 12.07641 12.08845 12.10035 12.11226 12.12429
## [297] 12.13657 12.14923 12.16241 12.17622 12.19005 12.20323 12.21584 12.22797
## [305] 12.23970 12.25111 12.26227 12.27328 12.28420 12.29513 12.30614 12.31731
## [313] 12.32873 12.34047 12.35262 12.36526 12.37945 12.39588 12.41411 12.43367
## [321] 12.45411 12.47497 12.49579 12.51611 12.53548 12.55344 12.56953 12.58329
## [329] 12.59574 12.60816 12.62048 12.63267 12.64465 12.65639 12.66782 12.67890
## [337] 12.68956 12.69976 12.70943 12.71853 12.72701 12.73480 12.74158 12.74714
## [345] 12.75164 12.75523 12.75805 12.76027 12.76203 12.76348 12.76477 12.76605
## [353] 12.76748 12.76920 12.77137 12.77413 12.77747 12.78119 12.78519 12.78937
## [361] 12.79362 12.79783 12.80190 12.80572 12.80919 12.81220 12.81464 12.81642
## [369] 12.81742 12.81754 12.81644 12.81403 12.81063 12.80652 12.80201 12.79741
## [377] 12.79299 12.78908 12.78596 12.78219 12.77638 12.76895 12.76032 12.75091
## [385] 12.74113 12.73139 12.72212 12.71373 12.70663 12.70125 12.69799 12.69728
## [393] 12.69953 12.70339 12.70730 12.71137 12.71568 12.72034 12.72544 12.73108
## [401] 12.73736 12.74437 12.75222 12.76100 12.77081 12.78137 12.79237 12.80385
## [409] 12.81582 12.82833 12.84139 12.85505 12.86933 12.88426 12.89987 12.91619
## [417] 12.93326 12.95109 12.96973 12.98935 13.01005 13.03174 13.05436 13.07783
## [425] 13.10206 13.12698 13.15250 13.17856
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 428)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 10.82073 10.90151 10.98091 11.05889 11.13543 11.21051 11.28409 11.35615
## [9] 11.42667 11.49562 11.56298 11.62872 11.69281 11.75523 11.81596 11.87502
## [17] 11.93248 11.98837 12.04270 12.09551 12.14682 12.19664 12.24502 12.29196
## [25] 12.33750 12.38165 12.42445 12.46591 12.50607 12.54458 12.58116 12.61590
## [33] 12.64888 12.68021 12.70996 12.73824 12.76513 12.79072 12.81511 12.83839
## [41] 12.86064 12.88195 12.90243 12.92071 12.93565 12.94768 12.95721 12.96468
## [49] 12.97051 12.97511 12.97891 12.98233 12.98580 12.98974 12.99457 12.99915
## [57] 13.00216 13.00373 13.00397 13.00301 13.00096 12.99796 12.99413 12.98959
## [65] 12.98445 12.97886 12.97292 12.96677 12.96052 12.95429 12.94757 12.93981
## [73] 12.93109 12.92151 12.91115 12.90009 12.88843 12.87625 12.86365 12.85070
## [81] 12.83751 12.82414 12.81070 12.79709 12.78314 12.76881 12.75406 12.73886
## [89] 12.72315 12.70692 12.69010 12.67268 12.65460 12.63583 12.61633 12.59606
## [97] 12.57499 12.54962 12.51777 12.48129 12.44206 12.40197 12.36288 12.32666
## [105] 12.29520 12.27037 12.24719 12.22001 12.18963 12.15690 12.12263 12.08764
## [113] 12.05276 12.01881 11.98661 11.95699 11.93076 11.90877 11.89181 11.88073
## [121] 11.87425 11.87039 11.86889 11.86949 11.87194 11.87598 11.88135 11.88780
## [129] 11.89507 11.90291 11.91105 11.91925 11.92724 11.93477 11.94364 11.95563
## [137] 11.97048 11.98789 12.00760 12.02933 12.05281 12.07775 12.10388 12.13093
## [145] 12.15861 12.18665 12.21478 12.24271 12.27018 12.29690 12.32259 12.34699
## [153] 12.36982 12.39549 12.42744 12.46389 12.50309 12.54325 12.58260 12.61939
## [161] 12.65183 12.67816 12.70139 12.72559 12.75058 12.77615 12.80213 12.82832
## [169] 12.85453 12.88057 12.90626 12.93140 12.95580 12.97928 13.00165 13.02271
## [177] 13.04227 13.06016 13.07617 13.09011 13.10181 13.11382 13.12844 13.14506
## [185] 13.16306 13.18183 13.20077 13.21926 13.23668 13.25243 13.26590 13.27648
## [193] 13.28354 13.28649 13.28471 13.27896 13.27056 13.25971 13.24662 13.23150
## [201] 13.21455 13.19597 13.17598 13.15478 13.13257 13.10956 13.08596 13.06197
## [209] 13.03780 13.00904 12.97247 12.93005 12.88376 12.83559 12.78750 12.74147
## [217] 12.69949 12.66351 12.62906 12.59073 12.54915 12.50496 12.45879 12.41126
## [225] 12.36301 12.31467 12.26687 12.22023 12.17540 12.13301 12.09367 12.05803
## [233] 12.02264 11.98414 11.94323 11.90062 11.85704 11.81318 11.76978 11.72753
## [241] 11.68716 11.64937 11.61488 11.58441 11.55866 11.53607 11.51463 11.49439
## [249] 11.47540 11.45770 11.44136 11.42642 11.41294 11.40096 11.39053 11.38170
## [257] 11.37453 11.36907 11.36536 11.36346 11.36477 11.37026 11.37929 11.39126
## [265] 11.40554 11.42152 11.43857 11.45608 11.47343 11.49000 11.50516 11.51831
## [273] 11.53151 11.54708 11.56471 11.58411 11.60498 11.62704 11.64998 11.67351
## [281] 11.69734 11.72117 11.74471 11.76766 11.78974 11.81063 11.83244 11.85714
## [289] 11.88427 11.91336 11.94396 11.97560 12.00783 12.04018 12.07218 12.10338
## [297] 12.13332 12.16153 12.18755 12.21093 12.23268 12.25414 12.27531 12.29617
## [305] 12.31672 12.33695 12.35685 12.37642 12.39565 12.41454 12.43307 12.45124
## [313] 12.46905 12.48648 12.50353 12.52019 12.53657 12.55278 12.56878 12.58456
## [321] 12.60008 12.61533 12.63027 12.64489 12.65917 12.67307 12.68657 12.69965
## [329] 12.71211 12.72382 12.73486 12.74529 12.75520 12.76466 12.77376 12.78256
## [337] 12.79114 12.79958 12.80796 12.81635 12.82483 12.83347 12.84178 12.84927
## [345] 12.85607 12.86227 12.86799 12.87334 12.87843 12.88338 12.88829 12.89328
## [353] 12.89845 12.90392 12.90980 12.91620 12.92317 12.93063 12.93846 12.94658
## [361] 12.95489 12.96327 12.97164 12.97988 12.98791 12.99562 13.00291 13.00968
## [369] 13.01584 13.02127 13.02592 13.02989 13.03335 13.03645 13.03935 13.04220
## [377] 13.04516 13.04839 13.05205 13.05580 13.05927 13.06249 13.06552 13.06840
## [385] 13.07118 13.07391 13.07663 13.07940 13.08226 13.08527 13.08846 13.09188
## [393] 13.09559 13.09944 13.10325 13.10702 13.11076 13.11445 13.11810 13.12170
## [401] 13.12525 13.12874 13.13219 13.13557 13.13890 13.14215 13.14533 13.14843
## [409] 13.15147 13.15445 13.15739 13.16028 13.16314 13.16598 13.16879 13.17159
## [417] 13.17439 13.17719 13.18000 13.18284 13.18570 13.18859 13.19149 13.19438
## [425] 13.19727 13.20012 13.20295 13.20572
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 428)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 10.86192 10.92146 10.97999 11.03751 11.09403 11.14953 11.20402 11.25748
## [9] 11.30991 11.36132 11.41168 11.46101 11.50930 11.55653 11.60272 11.64788
## [17] 11.69206 11.73525 11.77744 11.81864 11.85882 11.89799 11.93614 11.97327
## [25] 12.00938 12.04444 12.07847 12.11145 12.14338 12.17414 12.20364 12.23192
## [33] 12.25902 12.28499 12.30987 12.33370 12.35652 12.37838 12.39931 12.41936
## [41] 12.43858 12.45700 12.47467 12.49156 12.50761 12.52282 12.53717 12.55066
## [49] 12.56327 12.57500 12.58585 12.59579 12.60482 12.61293 12.62012 12.62632
## [57] 12.63150 12.63568 12.63888 12.64113 12.64243 12.64281 12.64230 12.64091
## [65] 12.63867 12.63559 12.63169 12.62700 12.62154 12.61533 12.60692 12.59521
## [73] 12.58069 12.56386 12.54523 12.52530 12.50457 12.48355 12.46273 12.44261
## [81] 12.42371 12.40651 12.39153 12.37644 12.35889 12.33930 12.31809 12.29570
## [89] 12.27254 12.24904 12.22564 12.20274 12.18079 12.16021 12.14141 12.12484
## [97] 12.11091 12.09909 12.08842 12.07864 12.06951 12.06080 12.05225 12.04362
## [105] 12.03467 12.02516 12.01562 12.00671 11.99840 11.99063 11.98336 11.97652
## [113] 11.97009 11.96400 11.95821 11.95267 11.94733 11.94214 11.93706 11.93203
## [121] 11.92719 11.92269 11.91849 11.91457 11.91090 11.90746 11.90422 11.90115
## [129] 11.89823 11.89543 11.89272 11.89007 11.88746 11.88487 11.88145 11.87656
## [137] 11.87044 11.86334 11.85552 11.84721 11.83868 11.83017 11.82192 11.81419
## [145] 11.80723 11.80128 11.79660 11.79344 11.79203 11.79264 11.79551 11.80089
## [153] 11.80903 11.81966 11.83213 11.84612 11.86131 11.87736 11.89396 11.91079
## [161] 11.92751 11.94380 11.96224 11.98525 12.01232 12.04290 12.07649 12.11255
## [169] 12.15056 12.18999 12.23032 12.27103 12.31158 12.35146 12.39013 12.42708
## [177] 12.46177 12.49369 12.52230 12.54709 12.56752 12.58816 12.61330 12.64202
## [185] 12.67339 12.70648 12.74038 12.77414 12.80686 12.83760 12.86543 12.88944
## [193] 12.90870 12.92228 12.92925 12.93081 12.92898 12.92407 12.91638 12.90622
## [201] 12.89391 12.87974 12.86403 12.84709 12.82923 12.81075 12.79196 12.77317
## [209] 12.75470 12.73049 12.69610 12.65416 12.60735 12.55832 12.50974 12.46426
## [217] 12.42455 12.39326 12.36434 12.33051 12.29265 12.25163 12.20830 12.16352
## [225] 12.11818 12.07312 12.02921 11.98732 11.94832 11.91306 11.88241 11.85724
## [233] 11.83555 11.81476 11.79486 11.77583 11.75766 11.74033 11.72382 11.70813
## [241] 11.69324 11.67912 11.66577 11.65317 11.64131 11.63106 11.62316 11.61737
## [249] 11.61345 11.61115 11.61025 11.61051 11.61168 11.61353 11.61582 11.61831
## [257] 11.62077 11.62295 11.62461 11.62553 11.62651 11.62849 11.63139 11.63514
## [265] 11.63966 11.64489 11.65074 11.65714 11.66402 11.67130 11.67891 11.68678
## [273] 11.69517 11.70433 11.71418 11.72460 11.73551 11.74680 11.75838 11.77014
## [281] 11.78200 11.79384 11.80558 11.81712 11.82835 11.83918 11.85048 11.86306
## [289] 11.87674 11.89134 11.90667 11.92256 11.93882 11.95528 11.97174 11.98804
## [297] 12.00397 12.01938 12.03407 12.04786 12.06207 12.07796 12.09527 12.11372
## [305] 12.13306 12.15303 12.17335 12.19376 12.21400 12.23381 12.25291 12.27105
## [313] 12.28797 12.30338 12.31705 12.32868 12.33830 12.34625 12.35277 12.35810
## [321] 12.36250 12.36621 12.36948 12.37256 12.37568 12.37910 12.38306 12.38781
## [329] 12.39302 12.39815 12.40319 12.40810 12.41287 12.41746 12.42185 12.42602
## [337] 12.42993 12.43357 12.43690 12.43990 12.44255 12.44481 12.44630 12.44675
## [345] 12.44630 12.44510 12.44329 12.44101 12.43843 12.43568 12.43291 12.43026
## [353] 12.42789 12.42593 12.42454 12.42386 12.42284 12.42050 12.41706 12.41274
## [361] 12.40779 12.40241 12.39684 12.39131 12.38604 12.38126 12.37720 12.37408
## [369] 12.37213 12.37158 12.37186 12.37229 12.37291 12.37376 12.37488 12.37631
## [377] 12.37809 12.38027 12.38289 12.38614 12.39015 12.39482 12.40005 12.40575
## [385] 12.41182 12.41818 12.42472 12.43135 12.43799 12.44452 12.45086 12.45692
## [393] 12.46260 12.46824 12.47421 12.48047 12.48698 12.49367 12.50050 12.50744
## [401] 12.51441 12.52139 12.52832 12.53515 12.54184 12.54849 12.55523 12.56207
## [409] 12.56899 12.57600 12.58309 12.59026 12.59751 12.60483 12.61223 12.61969
## [417] 12.62722 12.63482 12.64247 12.65014 12.65778 12.66542 12.67308 12.68077
## [425] 12.68850 12.69630 12.70418 12.71216
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")
keeping in case
#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")